﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace WMSSoft.WinKStart.ViewModel
{
    public class BaseTreeViewItemViewModel : INotifyPropertyChanged
    {
        ObservableCollection<BaseTreeViewItemViewModel> _Items = null;
        BaseTreeViewItemViewModel _parent = null;
        bool _isExpanded = false;
        bool _isSelected = false;

        #region Initialization

        public BaseTreeViewItemViewModel() 
        {
            _Items = new ObservableCollection<BaseTreeViewItemViewModel>();
        }

        public BaseTreeViewItemViewModel(BaseTreeViewItemViewModel Parent, ObservableCollection<BaseTreeViewItemViewModel> Items)
        {
            _Items = new ObservableCollection<BaseTreeViewItemViewModel>();
            _parent = Parent;
            _Items = Items;
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets/sets whether the TreeViewItem 
        /// associated with this object is expanded.
        /// </summary>
        public bool IsExpanded
        {
            get { return _isExpanded; }
            set
            {
                if (value != _isExpanded)
                {
                    _isExpanded = value;
                    this.OnPropertyChanged("IsExpanded");
                }

                // Expand all the way up to the root.
                if (_isExpanded && _parent != null)
                    _parent.IsExpanded = true;

            }
        }

        /// <summary>
        /// Gets/sets whether the TreeViewItem 
        /// associated with this object is selected.
        /// </summary>
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value != _isSelected)
                {
                    _isSelected = value;
                    this.OnPropertyChanged("IsSelected");
                }
            }
        }

        /// <summary>
        /// Returns the logical child items of this object.
        /// </summary>
        public ObservableCollection<BaseTreeViewItemViewModel> Items
        {
            get { return _Items; }
            set
            {
                _Items = value;
                OnPropertyChanged("Items");
            }
        }

        public BaseTreeViewItemViewModel Parent
        {
            get { return _parent; }
            set
            {
                _parent = value;
                OnPropertyChanged("Parent");
            }
        }

        #endregion
    }
}
